//
//  NewOrderV.swift
//  Employee
//
//  Created by swordjoy on 2017/3/27.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit
import Photos

class NewOrderV: UIView {
    
    var assets: [PHAsset] = [] {
        didSet {
            imageSelectV.assets = assets
        }
    }
  
  var addPhotoClosure: VoidClosure? {
    didSet {
      guard let closure = addPhotoClosure else { return }
      imageSelectV.addPhotoClosure = closure
    }
  }
  
  var deleteClosure: ( (Int) -> () )? {
    didSet {
      guard let closure = deleteClosure else { return }
      imageSelectV.deleteClosure = closure
    }
  }
  
  var cellDidClickAction: ( (Int) -> () )? {
    didSet {
      imageSelectV.cellDidClickAction = cellDidClickAction
    }
  }
  
  var titleDidEndEditing: ( (String) -> () )?
  var contentDidEndEditing: ( (String) -> () )?
  
  fileprivate var contentV: UIView!
  fileprivate var titleTextV: PlaceholderTextView!
  fileprivate var contentTextView: PlaceholderTextView!
  fileprivate var imageSelectV: ImageSelectV!
  fileprivate var tipLabel: UILabel!
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor.clear
    
    create()
  }
  
  override func layoutSubviews() {
    super.layoutSubviews()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

// MARK: - fileprivate

extension NewOrderV {
  
  fileprivate func create() {
    contentV = UIView()
    addSubview(contentV)
    contentV.backgroundColor = UIColor.white
    contentV.layer.cornerRadius = 4
    contentV.clipsToBounds = true
    contentV.snp.makeConstraints { (make) in
      make.left.top.equalTo(10)
      make.right.equalTo(-10)
      make.bottom.equalToSuperview()
    }
    
    titleTextV = PlaceholderTextView()
    contentV.addSubview(titleTextV)
    titleTextV.backgroundColor = UIColor.clear

    titleTextV.placeholderLabel.text = "点击输入工单标题(20字以内)"
    titleTextV.placeholderLabel.font = GlobalConfig.bold20Font
    titleTextV.placeholderLabel.textColor = GlobalConfig.TextLightColor
    titleTextV.textView.font = GlobalConfig.bold20Font
    titleTextV.textView.textColor = GlobalConfig.TextDeepColor
    titleTextV.isOldLevel = true
    titleTextV.maxInput = 20
    titleTextV.textViewDidEndEditing = { [weak self] (text) in
      if let closure = self?.titleDidEndEditing { closure(text) }
    }
    
    titleTextV.textViewEditing = { [weak self] (text) in
      var temp = text
      if temp.sj.length > 20 {
        temp = temp.sj.cut(to: 20)
        self?.titleTextV.textView.text = temp
      }
    }

    
    titleTextV.snp.makeConstraints { (make) in
      make.left.equalTo(15)
      make.right.equalTo(-15)
      make.height.equalTo(65)
      make.top.equalTo(20)
    }
    
    contentTextView = PlaceholderTextView()
    contentV.addSubview(contentTextView)
    contentTextView.backgroundColor = UIColor.clear
    contentTextView.isOldLevel = true
    contentTextView.placeholderLabel.text = "点击输入工单内容"
    contentTextView.placeholderLabel.font = GlobalConfig.system15Font
    contentTextView.placeholderLabel.textColor = GlobalConfig.TextLightColor
    contentTextView.textView.textColor = GlobalConfig.TextDeepColor
    contentTextView.textView.font = GlobalConfig.system15Font
    contentTextView.maxInput = 50
    contentTextView.textViewDidEndEditing = { [weak self] (text) in
      
      if let closure = self?.contentDidEndEditing {
        closure(text)
      }
      
    }
    
    contentTextView.snp.makeConstraints { (make) in
      make.left.equalTo(15)
      make.top.equalTo(titleTextV.snp.bottom).offset(25)
      make.right.equalTo(-15)
      make.height.equalTo(100)
    }
    
    let height = (UIScreen.width - 80) / 4
    imageSelectV = ImageSelectV()
    contentV.addSubview(imageSelectV)
    imageSelectV.snp.makeConstraints { (make) in
      make.top.equalTo(contentTextView.snp.bottom)
      make.left.right.equalToSuperview()
      make.height.equalTo(height + 30 + 15)
    }
    
    tipLabel = UILabel()
    contentV.addSubview(tipLabel)
    tipLabel.font = GlobalConfig.system15Font
    tipLabel.textColor = GlobalConfig.TextLightColor
    tipLabel.text = "最多可以添加4个附件"
    tipLabel.snp.makeConstraints { (make) in
      make.left.equalTo(15)
      make.top.equalTo(imageSelectV.snp.bottom)
    }

  }
}




